<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>CREATE TYPE</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="CREATE TRIGGER"
HREF="sql-createtrigger.html"><LINK
REL="NEXT"
TITLE="CREATE USER"
HREF="sql-createuser.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="CREATE TRIGGER"
HREF="sql-createtrigger.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="CREATE USER"
HREF="sql-createuser.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-CREATETYPE"
></A
>CREATE TYPE</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN68911"
></A
><H2
>Name</H2
>CREATE TYPE&nbsp;--&nbsp;define a new data type</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN68916"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CREATE TYPE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> AS
    ( [ <TT
CLASS="REPLACEABLE"
><I
>attribute_name</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>data_type</I
></TT
> [ COLLATE <TT
CLASS="REPLACEABLE"
><I
>collation</I
></TT
> ] [, ... ] ] )

CREATE TYPE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> AS ENUM
    ( [ '<TT
CLASS="REPLACEABLE"
><I
>label</I
></TT
>' [, ... ] ] )

CREATE TYPE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> (
    INPUT = <TT
CLASS="REPLACEABLE"
><I
>input_function</I
></TT
>,
    OUTPUT = <TT
CLASS="REPLACEABLE"
><I
>output_function</I
></TT
>
    [ , RECEIVE = <TT
CLASS="REPLACEABLE"
><I
>receive_function</I
></TT
> ]
    [ , SEND = <TT
CLASS="REPLACEABLE"
><I
>send_function</I
></TT
> ]
    [ , TYPMOD_IN = <TT
CLASS="REPLACEABLE"
><I
>type_modifier_input_function</I
></TT
> ]
    [ , TYPMOD_OUT = <TT
CLASS="REPLACEABLE"
><I
>type_modifier_output_function</I
></TT
> ]
    [ , ANALYZE = <TT
CLASS="REPLACEABLE"
><I
>analyze_function</I
></TT
> ]
    [ , INTERNALLENGTH = { <TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
> | VARIABLE } ]
    [ , PASSEDBYVALUE ]
    [ , ALIGNMENT = <TT
CLASS="REPLACEABLE"
><I
>alignment</I
></TT
> ]
    [ , STORAGE = <TT
CLASS="REPLACEABLE"
><I
>storage</I
></TT
> ]
    [ , LIKE = <TT
CLASS="REPLACEABLE"
><I
>like_type</I
></TT
> ]
    [ , CATEGORY = <TT
CLASS="REPLACEABLE"
><I
>category</I
></TT
> ]
    [ , PREFERRED = <TT
CLASS="REPLACEABLE"
><I
>preferred</I
></TT
> ]
    [ , DEFAULT = <TT
CLASS="REPLACEABLE"
><I
>default</I
></TT
> ]
    [ , ELEMENT = <TT
CLASS="REPLACEABLE"
><I
>element</I
></TT
> ]
    [ , DELIMITER = <TT
CLASS="REPLACEABLE"
><I
>delimiter</I
></TT
> ]
    [ , COLLATABLE = <TT
CLASS="REPLACEABLE"
><I
>collatable</I
></TT
> ]
)

CREATE TYPE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN68943"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> registers a new data type for use in
   the current database.  The user who defines a type becomes its
   owner.
  </P
><P
>   If a schema name is given then the type is created in the specified
   schema.  Otherwise it is created in the current schema.  The type
   name must be distinct from the name of any existing type or domain
   in the same schema.  (Because tables have associated data types,
   the type name must also be distinct from the name of any existing
   table in the same schema.)
  </P
><DIV
CLASS="REFSECT2"
><A
NAME="AEN68948"
></A
><H3
>Composite Types</H3
><P
>   The first form of <TT
CLASS="COMMAND"
>CREATE TYPE</TT
>
   creates a composite type.
   The composite type is specified by a list of attribute names and data types.
   An attribute's collation can be specified too, if its data type is
   collatable.  A composite type is essentially the same as the row type
   of a table, but using <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> avoids the need to
   create an actual table when all that is wanted is to define a type.
   A stand-alone composite type is useful, for example, as the argument or
   return type of a function.
  </P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="SQL-CREATETYPE-ENUM"
></A
><H3
>Enumerated Types</H3
><P
>    The second form of <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> creates an enumerated
    (enum) type, as described in <A
HREF="datatype-enum.html"
>Section 8.7</A
>.
    Enum types take a list of one or more quoted labels, each of which
    must be less than <TT
CLASS="SYMBOL"
>NAMEDATALEN</TT
> bytes long (64 in a standard
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> build).
   </P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN68960"
></A
><H3
>Base Types</H3
><P
>   The third form of <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> creates a new base type
   (scalar type).  To create a new base type, you must be a superuser.
   (This restriction is made because an erroneous type definition could
   confuse or even crash the server.)
  </P
><P
>   The parameters can appear in any order, not only that
   illustrated above, and most are optional.  You must register
   two or more functions (using <TT
CLASS="COMMAND"
>CREATE FUNCTION</TT
>) before
   defining the type.  The support functions
   <TT
CLASS="REPLACEABLE"
><I
>input_function</I
></TT
> and
   <TT
CLASS="REPLACEABLE"
><I
>output_function</I
></TT
>
   are required, while the functions
   <TT
CLASS="REPLACEABLE"
><I
>receive_function</I
></TT
>,
   <TT
CLASS="REPLACEABLE"
><I
>send_function</I
></TT
>,
   <TT
CLASS="REPLACEABLE"
><I
>type_modifier_input_function</I
></TT
>,
   <TT
CLASS="REPLACEABLE"
><I
>type_modifier_output_function</I
></TT
> and
   <TT
CLASS="REPLACEABLE"
><I
>analyze_function</I
></TT
>
   are optional.  Generally these functions have to be coded in C
   or another low-level language.
  </P
><P
>   The <TT
CLASS="REPLACEABLE"
><I
>input_function</I
></TT
>
   converts the type's external textual representation to the internal
   representation used by the operators and functions defined for the type.
   <TT
CLASS="REPLACEABLE"
><I
>output_function</I
></TT
>
   performs the reverse transformation.  The input function can be
   declared as taking one argument of type <TT
CLASS="TYPE"
>cstring</TT
>,
   or as taking three arguments of types
   <TT
CLASS="TYPE"
>cstring</TT
>, <TT
CLASS="TYPE"
>oid</TT
>, <TT
CLASS="TYPE"
>integer</TT
>.
   The first argument is the input text as a C string, the second
   argument is the type's own OID (except for array types, which instead
   receive their element type's OID),
   and the third is the <TT
CLASS="LITERAL"
>typmod</TT
> of the destination column, if known
   (-1 will be passed if not).
   The input function must return a value of the data type itself.
   Usually, an input function should be declared STRICT; if it is not,
   it will be called with a NULL first parameter when reading a NULL
   input value.  The function must still return NULL in this case, unless
   it raises an error.
   (This case is mainly meant to support domain input functions, which
   might need to reject NULL inputs.)
   The output function must be
   declared as taking one argument of the new data type.
   The output function must return type <TT
CLASS="TYPE"
>cstring</TT
>.
   Output functions are not invoked for NULL values.
  </P
><P
>   The optional <TT
CLASS="REPLACEABLE"
><I
>receive_function</I
></TT
>
   converts the type's external binary representation to the internal
   representation.  If this function is not supplied, the type cannot
   participate in binary input.  The binary representation should be
   chosen to be cheap to convert to internal form, while being reasonably
   portable.  (For example, the standard integer data types use network
   byte order as the external binary representation, while the internal
   representation is in the machine's native byte order.)  The receive
   function should perform adequate checking to ensure that the value is
   valid.
   The receive function can be declared as taking one argument of type
   <TT
CLASS="TYPE"
>internal</TT
>, or as taking three arguments of types
   <TT
CLASS="TYPE"
>internal</TT
>, <TT
CLASS="TYPE"
>oid</TT
>, <TT
CLASS="TYPE"
>integer</TT
>.
   The first argument is a pointer to a <TT
CLASS="TYPE"
>StringInfo</TT
> buffer
   holding the received byte string; the optional arguments are the
   same as for the text input function.
   The receive function must return a value of the data type itself.
   Usually, a receive function should be declared STRICT; if it is not,
   it will be called with a NULL first parameter when reading a NULL
   input value.  The function must still return NULL in this case, unless
   it raises an error.
   (This case is mainly meant to support domain receive functions, which
   might need to reject NULL inputs.)
   Similarly, the optional
   <TT
CLASS="REPLACEABLE"
><I
>send_function</I
></TT
> converts
   from the internal representation to the external binary representation.
   If this function is not supplied, the type cannot participate in binary
   output.  The send function must be
   declared as taking one argument of the new data type.
   The send function must return type <TT
CLASS="TYPE"
>bytea</TT
>.
   Send functions are not invoked for NULL values.
  </P
><P
>   You should at this point be wondering how the input and output functions
   can be declared to have results or arguments of the new type, when they
   have to be created before the new type can be created.  The answer is that
   the type should first be defined as a <I
CLASS="FIRSTTERM"
>shell type</I
>, which is a
   placeholder type that has no properties except a name and an owner.  This
   is done by issuing the command <TT
CLASS="LITERAL"
>CREATE TYPE
   <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></TT
>, with no additional parameters.  Then the
   I/O functions can be defined referencing the shell type.  Finally,
   <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> with a full definition replaces the shell entry
   with a complete, valid type definition, after which the new type can be
   used normally.
  </P
><P
>   The optional
   <TT
CLASS="REPLACEABLE"
><I
>type_modifier_input_function</I
></TT
>
   and <TT
CLASS="REPLACEABLE"
><I
>type_modifier_output_function</I
></TT
>
   are needed if the type supports modifiers, that is optional constraints
   attached to a type declaration, such as <TT
CLASS="LITERAL"
>char(5)</TT
> or
   <TT
CLASS="LITERAL"
>numeric(30,2)</TT
>.  <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> allows
   user-defined types to take one or more simple constants or identifiers as
   modifiers.  However, this information must be capable of being packed into a
   single non-negative integer value for storage in the system catalogs.  The
   <TT
CLASS="REPLACEABLE"
><I
>type_modifier_input_function</I
></TT
>
   is passed the declared modifier(s) in the form of a <TT
CLASS="TYPE"
>cstring</TT
>
   array.  It must check the values for validity (throwing an error if they
   are wrong), and if they are correct, return a single non-negative
   <TT
CLASS="TYPE"
>integer</TT
> value that will be stored as the column <SPAN
CLASS="QUOTE"
>"typmod"</SPAN
>.
   Type modifiers will be rejected if the type does not have a
   <TT
CLASS="REPLACEABLE"
><I
>type_modifier_input_function</I
></TT
>.
   The <TT
CLASS="REPLACEABLE"
><I
>type_modifier_output_function</I
></TT
>
   converts the internal integer typmod value back to the correct form for
   user display.  It must return a <TT
CLASS="TYPE"
>cstring</TT
> value that is the exact
   string to append to the type name; for example <TT
CLASS="TYPE"
>numeric</TT
>'s
   function might return <TT
CLASS="LITERAL"
>(30,2)</TT
>.
   It is allowed to omit the
   <TT
CLASS="REPLACEABLE"
><I
>type_modifier_output_function</I
></TT
>,
   in which case the default display format is just the stored typmod integer
   value enclosed in parentheses.
  </P
><P
>   The optional <TT
CLASS="REPLACEABLE"
><I
>analyze_function</I
></TT
>
   performs type-specific statistics collection for columns of the data type.
   By default, <TT
CLASS="COMMAND"
>ANALYZE</TT
> will attempt to gather statistics using
   the type's <SPAN
CLASS="QUOTE"
>"equals"</SPAN
> and <SPAN
CLASS="QUOTE"
>"less-than"</SPAN
> operators, if there
   is a default b-tree operator class for the type.  For non-scalar types
   this behavior is likely to be unsuitable, so it can be overridden by
   specifying a custom analysis function.  The analysis function must be
   declared to take a single argument of type <TT
CLASS="TYPE"
>internal</TT
>, and return
   a <TT
CLASS="TYPE"
>boolean</TT
> result.  The detailed API for analysis functions appears
   in <TT
CLASS="FILENAME"
>src/include/commands/vacuum.h</TT
>.
  </P
><P
>   While the details of the new type's internal representation are only
   known to the I/O functions and other functions you create to work with
   the type, there are several properties of the internal representation
   that must be declared to <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
   Foremost of these is
   <TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
>.
   Base data types can be fixed-length, in which case
   <TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
> is a
   positive integer, or variable  length, indicated by setting
   <TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
>
   to <TT
CLASS="LITERAL"
>VARIABLE</TT
>.  (Internally, this is represented
   by setting <TT
CLASS="LITERAL"
>typlen</TT
> to -1.)  The internal representation of all
   variable-length types must start with a 4-byte integer giving the total
   length of this value of the type.
  </P
><P
>   The optional flag <TT
CLASS="LITERAL"
>PASSEDBYVALUE</TT
> indicates that
   values of this data type are passed by value, rather than by
   reference.  You cannot pass by value types whose internal
   representation is larger than the size of the <TT
CLASS="TYPE"
>Datum</TT
> type
   (4 bytes on most machines, 8 bytes on a few).
  </P
><P
>   The <TT
CLASS="REPLACEABLE"
><I
>alignment</I
></TT
> parameter
   specifies the storage alignment required for the data type.  The
   allowed values equate to alignment on 1, 2, 4, or 8 byte boundaries.
   Note that variable-length types must have an alignment of at least
   4, since they necessarily contain an <TT
CLASS="TYPE"
>int4</TT
> as their first component.
  </P
><P
>   The <TT
CLASS="REPLACEABLE"
><I
>storage</I
></TT
> parameter
   allows selection of storage strategies for variable-length data
   types.  (Only <TT
CLASS="LITERAL"
>plain</TT
> is allowed for fixed-length
   types.)  <TT
CLASS="LITERAL"
>plain</TT
> specifies that data of the type
   will always be stored in-line and not compressed.
   <TT
CLASS="LITERAL"
>extended</TT
> specifies that the system will first
   try to compress a long data value, and will move the value out of
   the main table row if it's still too long.
   <TT
CLASS="LITERAL"
>external</TT
> allows the value to be moved out of the
   main table, but the system will not try to compress it.
   <TT
CLASS="LITERAL"
>main</TT
> allows compression, but discourages moving
   the value out of the main table.  (Data items with this storage
   strategy might still be moved out of the main table if there is no
   other way to make a row fit, but they will be kept in the main
   table preferentially over <TT
CLASS="LITERAL"
>extended</TT
> and
   <TT
CLASS="LITERAL"
>external</TT
> items.)
  </P
><P
>   The <TT
CLASS="REPLACEABLE"
><I
>like_type</I
></TT
> parameter
   provides an alternative method for specifying the basic representation
   properties of a data type: copy them from some existing type. The values of
   <TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
>,
   <TT
CLASS="REPLACEABLE"
><I
>passedbyvalue</I
></TT
>,
   <TT
CLASS="REPLACEABLE"
><I
>alignment</I
></TT
>, and
   <TT
CLASS="REPLACEABLE"
><I
>storage</I
></TT
> are copied from the
   named type.  (It is possible, though usually undesirable, to override
   some of these values by specifying them along with the <TT
CLASS="LITERAL"
>LIKE</TT
>
   clause.)  Specifying representation this way is especially useful when
   the low-level implementation of the new type <SPAN
CLASS="QUOTE"
>"piggybacks"</SPAN
> on an
   existing type in some fashion.
  </P
><P
>   The <TT
CLASS="REPLACEABLE"
><I
>category</I
></TT
> and
   <TT
CLASS="REPLACEABLE"
><I
>preferred</I
></TT
> parameters can be
   used to help control which implicit cast will be applied in ambiguous
   situations.  Each data type belongs to a category named by a single ASCII
   character, and each type is either <SPAN
CLASS="QUOTE"
>"preferred"</SPAN
> or not within its
   category.  The parser will prefer casting to preferred types (but only from
   other types within the same category) when this rule is helpful in
   resolving overloaded functions or operators.  For more details see <A
HREF="typeconv.html"
>Chapter 10</A
>.  For types that have no implicit casts to or from any
   other types, it is sufficient to leave these settings at the defaults.
   However, for a group of related types that have implicit casts, it is often
   helpful to mark them all as belonging to a category and select one or two
   of the <SPAN
CLASS="QUOTE"
>"most general"</SPAN
> types as being preferred within the category.
   The <TT
CLASS="REPLACEABLE"
><I
>category</I
></TT
> parameter is
   especially useful when adding a user-defined type to an existing built-in
   category, such as the numeric or string types.  However, it is also
   possible to create new entirely-user-defined type categories.  Select any
   ASCII character other than an upper-case letter to name such a category.
  </P
><P
>   A default value can be specified, in case a user wants columns of the
   data type to default to something other than the null value.
   Specify the default with the <TT
CLASS="LITERAL"
>DEFAULT</TT
> key word.
   (Such a default can be overridden by an explicit <TT
CLASS="LITERAL"
>DEFAULT</TT
>
   clause attached to a particular column.)
  </P
><P
>   To indicate that a type is an array, specify the type of the array
   elements using the <TT
CLASS="LITERAL"
>ELEMENT</TT
> key word.  For example, to
   define an array of 4-byte integers (<TT
CLASS="TYPE"
>int4</TT
>), specify
   <TT
CLASS="LITERAL"
>ELEMENT = int4</TT
>. More details about array types
   appear below.
  </P
><P
>   To indicate the delimiter to be used between values in the external
   representation of arrays of this type, <TT
CLASS="REPLACEABLE"
><I
>delimiter</I
></TT
> can be
   set to a specific character.  The default delimiter is the comma
   (<TT
CLASS="LITERAL"
>,</TT
>).  Note that the delimiter is associated
   with the array element type, not the array type itself.
  </P
><P
>   If the optional Boolean
   parameter <TT
CLASS="REPLACEABLE"
><I
>collatable</I
></TT
>
   is true, column definitions and expressions of the type may carry
   collation information through use of
   the <TT
CLASS="LITERAL"
>COLLATE</TT
> clause.  It is up to the
   implementations of the functions operating on the type to actually
   make use of the collation information; this does not happen
   automatically merely by marking the type collatable.
  </P
></DIV
><DIV
CLASS="REFSECT2"
><A
NAME="AEN69070"
></A
><H3
>Array Types</H3
><P
>    Whenever a user-defined type is created,
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> automatically creates an
    associated array type, whose name consists of the base type's
    name prepended with an underscore, and truncated if necessary to keep
    it less than <TT
CLASS="SYMBOL"
>NAMEDATALEN</TT
> bytes long.  (If the name
    so generated collides with an existing type name, the process is
    repeated until a non-colliding name is found.)
    This implicitly-created array type is variable length and uses the
    built-in input and output functions <TT
CLASS="LITERAL"
>array_in</TT
> and
    <TT
CLASS="LITERAL"
>array_out</TT
>.  The array type tracks any changes in its
    element type's owner or schema, and is dropped if the element type is.
   </P
><P
>    You might reasonably ask why there is an <TT
CLASS="OPTION"
>ELEMENT</TT
>
    option, if the system makes the correct array type automatically.
    The only case where it's useful to use <TT
CLASS="OPTION"
>ELEMENT</TT
> is when you are
    making a fixed-length type that happens to be internally an array of a number of
    identical things, and you want to allow these things to be accessed
    directly by subscripting, in addition to whatever operations you plan
    to provide for the type as a whole.  For example, type <TT
CLASS="TYPE"
>point</TT
>
    is represented as just two floating-point numbers, which it allows to be
    accessed as <TT
CLASS="LITERAL"
>point[0]</TT
> and <TT
CLASS="LITERAL"
>point[1]</TT
>.
    Note that
    this facility only works for fixed-length types whose internal form
    is exactly a sequence of identical fixed-length fields.  A subscriptable
    variable-length type must have the generalized internal representation
    used by <TT
CLASS="LITERAL"
>array_in</TT
> and <TT
CLASS="LITERAL"
>array_out</TT
>.
    For historical reasons (i.e., this is clearly wrong but it's far too
    late to change it), subscripting of fixed-length array types starts from
    zero, rather than from one as for variable-length arrays.
   </P
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69085"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of a type to be created.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>attribute_name</I
></TT
></DT
><DD
><P
>      The name of an attribute (column) for the composite type.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>data_type</I
></TT
></DT
><DD
><P
>      The name of an existing data type to become a column of the
      composite type.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>label</I
></TT
></DT
><DD
><P
>      A string literal representing the textual label associated with
      one value of an enum type.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>input_function</I
></TT
></DT
><DD
><P
>      The name of a function that converts data from the type's
      external textual form to its internal form.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>output_function</I
></TT
></DT
><DD
><P
>      The name of a function that converts data from the type's
      internal form to its external textual form.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>receive_function</I
></TT
></DT
><DD
><P
>      The name of a function that converts data from the type's
      external binary form to its internal form.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>send_function</I
></TT
></DT
><DD
><P
>      The name of a function that converts data from the type's
      internal form to its external binary form.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>type_modifier_input_function</I
></TT
></DT
><DD
><P
>      The name of a function that converts an array of modifier(s) for the type
      into internal form.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>type_modifier_output_function</I
></TT
></DT
><DD
><P
>      The name of a function that converts the internal form of the type's
      modifier(s) to external textual form.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>analyze_function</I
></TT
></DT
><DD
><P
>      The name of a function that performs statistical analysis for the
      data type.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
></DT
><DD
><P
>      A numeric constant that specifies the length in bytes of the new
      type's internal representation.  The default assumption is that
      it is variable-length.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>alignment</I
></TT
></DT
><DD
><P
>      The storage alignment requirement of the data type.  If specified,
      it must be <TT
CLASS="LITERAL"
>char</TT
>, <TT
CLASS="LITERAL"
>int2</TT
>,
      <TT
CLASS="LITERAL"
>int4</TT
>, or <TT
CLASS="LITERAL"
>double</TT
>; the
      default is <TT
CLASS="LITERAL"
>int4</TT
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>storage</I
></TT
></DT
><DD
><P
>      The storage strategy for the data type.  If specified, must be
      <TT
CLASS="LITERAL"
>plain</TT
>, <TT
CLASS="LITERAL"
>external</TT
>,
      <TT
CLASS="LITERAL"
>extended</TT
>, or <TT
CLASS="LITERAL"
>main</TT
>; the
      default is <TT
CLASS="LITERAL"
>plain</TT
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>like_type</I
></TT
></DT
><DD
><P
>      The name of an existing data type that the new type will have the
      same representation as.  The values of
      <TT
CLASS="REPLACEABLE"
><I
>internallength</I
></TT
>,
      <TT
CLASS="REPLACEABLE"
><I
>passedbyvalue</I
></TT
>,
      <TT
CLASS="REPLACEABLE"
><I
>alignment</I
></TT
>, and
      <TT
CLASS="REPLACEABLE"
><I
>storage</I
></TT
>
      are copied from that type, unless overridden by explicit
      specification elsewhere in this <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> command.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>category</I
></TT
></DT
><DD
><P
>      The category code (a single ASCII character) for this type.
      The default is <TT
CLASS="LITERAL"
>'U'</TT
> for <SPAN
CLASS="QUOTE"
>"user-defined type"</SPAN
>.
      Other standard category codes can be found in
      <A
HREF="catalog-pg-type.html#CATALOG-TYPCATEGORY-TABLE"
>Table 45-49</A
>.  You may also choose
      other ASCII characters in order to create custom categories.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>preferred</I
></TT
></DT
><DD
><P
>      True if this type is a preferred type within its type category,
      else false.  The default is false.  Be very careful about creating
      a new preferred type within an existing type category, as this
      could cause surprising changes in behavior.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>default</I
></TT
></DT
><DD
><P
>      The default value for the data type.  If this is omitted, the
      default is null.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>element</I
></TT
></DT
><DD
><P
>      The type being created is an array; this specifies the type of
      the array elements.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>delimiter</I
></TT
></DT
><DD
><P
>      The delimiter character to be used between values in arrays made
      of this type.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>collatable</I
></TT
></DT
><DD
><P
>      True if this type's operations can use collation information.
      The default is false.
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="SQL-CREATETYPE-NOTES"
></A
><H2
>Notes</H2
><P
>   Because there are no restrictions on use of a data type once it's been
   created, creating a base type is tantamount to granting public execute
   permission on the functions mentioned in the type definition.
   This is usually
   not an issue for the sorts of functions that are useful in a type
   definition.  But you might want to think twice before designing a type
   in a way that would require <SPAN
CLASS="QUOTE"
>"secret"</SPAN
> information to be used
   while converting it to or from external form.
  </P
><P
>   Before <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> version 8.3, the name of
   a generated array type was always exactly the element type's name with one
   underscore character (<TT
CLASS="LITERAL"
>_</TT
>) prepended.  (Type names were
   therefore restricted in length to one less character than other names.)
   While this is still usually the case, the array type name may vary from
   this in case of maximum-length names or collisions with user type names
   that begin with underscore.  Writing code that depends on this convention
   is therefore deprecated.  Instead, use
   <TT
CLASS="STRUCTNAME"
>pg_type</TT
>.<TT
CLASS="STRUCTFIELD"
>typarray</TT
> to locate the array type
   associated with a given type.
  </P
><P
>   It may be advisable to avoid using type and table names that begin with
   underscore.  While the server will change generated array type names to
   avoid collisions with user-given names, there is still risk of confusion,
   particularly with old client software that may assume that type names
   beginning with underscores always represent arrays.
  </P
><P
>   Before <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> version 8.2, the syntax
   <TT
CLASS="LITERAL"
>CREATE TYPE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></TT
> did not exist.
   The way to create a new base type was to create its input function first.
   In this approach, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> will first see
   the name of the new data type as the return type of the input function.
   The shell type is implicitly created in this situation, and then it
   can be referenced in the definitions of the remaining I/O functions.
   This approach still works, but is deprecated and might be disallowed in
   some future release.  Also, to avoid accidentally cluttering
   the catalogs with shell types as a result of simple typos in function
   definitions, a shell type will only be made this way when the input
   function is written in C.
  </P
><P
>   In <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> versions before 7.3, it
   was customary to avoid creating a shell type at all, by replacing the
   functions' forward references to the type name with the placeholder
   pseudotype <TT
CLASS="TYPE"
>opaque</TT
>.  The <TT
CLASS="TYPE"
>cstring</TT
> arguments and
   results also had to be declared as <TT
CLASS="TYPE"
>opaque</TT
> before 7.3.  To
   support loading of old dump files, <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> will
   accept I/O functions declared using <TT
CLASS="TYPE"
>opaque</TT
>, but it will issue
   a notice and change the function declarations to use the correct
   types.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN69233"
></A
><H2
>Examples</H2
><P
>   This example creates a composite type and uses it in
   a function definition:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE compfoo AS (f1 int, f2 text);

CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$
    SELECT fooid, fooname FROM foo
$$ LANGUAGE SQL;</PRE
><P>
  </P
><P
>   This example creates an enumerated type and uses it in
   a table definition:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');

CREATE TABLE bug (
    id serial,
    description text,
    status bug_status
);</PRE
><P>
  </P
><P
>   This example creates the base data type <TT
CLASS="TYPE"
>box</TT
> and then uses the
   type in a table definition:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE box;

CREATE FUNCTION my_box_in_function(cstring) RETURNS box AS ... ;
CREATE FUNCTION my_box_out_function(box) RETURNS cstring AS ... ;

CREATE TYPE box (
    INTERNALLENGTH = 16,
    INPUT = my_box_in_function,
    OUTPUT = my_box_out_function
);

CREATE TABLE myboxes (
    id integer,
    description box
);</PRE
><P>
  </P
><P
>   If the internal structure of <TT
CLASS="TYPE"
>box</TT
> were an array of four
   <TT
CLASS="TYPE"
>float4</TT
> elements, we might instead use:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE box (
    INTERNALLENGTH = 16,
    INPUT = my_box_in_function,
    OUTPUT = my_box_out_function,
    ELEMENT = float4
);</PRE
><P>
   which would allow a box value's component numbers to be accessed
   by subscripting.  Otherwise the type behaves the same as before.
  </P
><P
>   This example creates a large object type and uses it in
   a table definition:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE bigobj (
    INPUT = lo_filein, OUTPUT = lo_fileout,
    INTERNALLENGTH = VARIABLE
);
CREATE TABLE big_objs (
    id integer,
    obj bigobj
);</PRE
><P>
  </P
><P
>   More examples, including suitable input and output functions, are
   in <A
HREF="xtypes.html"
>Section 35.11</A
>.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="SQL-CREATETYPE-COMPATIBILITY"
></A
><H2
>Compatibility</H2
><P
>   The first form of the <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> command, which
   creates a composite type, conforms to the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard.
   The other forms are <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
   extensions.  The <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> statement in
   the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard also defines other forms that are not
   implemented in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
  </P
><P
>   The ability to create a composite type with zero attributes is
   a <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>-specific deviation from the
   standard (analogous to <TT
CLASS="COMMAND"
>CREATE TABLE</TT
>).
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="SQL-CREATETYPE-SEE-ALSO"
></A
><H2
>See Also</H2
><A
HREF="sql-altertype.html"
>ALTER TYPE</A
>, <A
HREF="sql-createdomain.html"
>CREATE DOMAIN</A
>, <A
HREF="sql-createfunction.html"
>CREATE FUNCTION</A
>, <A
HREF="sql-droptype.html"
>DROP TYPE</A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createtrigger.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createuser.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>CREATE TRIGGER</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>CREATE USER</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>